package com.stu.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbUtil {
	private String dbUrl = "jdbc:derby:D:/db_stusys;create=false";
	//数据库连接地址
	private String dbDriver = "org.apache.derby.jdbc.EmbeddedDriver";
	//驱动名称
	/**
	 ** @author Fairy
	 ** 获取数据库连接
	 */
	public Connection getCon() {
		try {
			Class.forName(dbDriver);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Connection con = null;
		try {
			con = DriverManager.getConnection(dbUrl);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	/**
	 ** @author Fairy
	 ** 关闭数据库连接
	 */
	public void closeCon(Connection con) throws SQLException {
		if (con != null) {
			con.close();
		}
	}
	
	public static void main(String[] args) {
		DbUtil dbUtil = new DbUtil();
		try {
			dbUtil.getCon();
			System.out.println("数据库连接成功");
		} catch (Exception e) {
			System.out.println("数据库连接失败");
		}
	}
}
